/**
 * 回访详情  初始化
 */
var ReturnVisit = {
    id: "ReturnVisitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

/**
 * 初始化表格的列
 */
ReturnVisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true},
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                }else{
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true},
        {title: '门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true},
        {title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {
            title: '首次到店', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 120
        },
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true},
        {title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true, width: 450},
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

/**
 * 查询 回访详情列表
 */
ReturnVisit.search = function () {
    ReturnVisit.table.refresh({query: ReturnVisit.formParams()});
};

/**
 * 重置
 */
ReturnVisit.resetSearch = function () {
    $("#outpatientName").val(""); //  门诊
    $("#sreach").val();  //姓名   电话 微信 预约号
    $("#startTime").val(""); //开始时间
    $("#endTime").val("");  //结束时间
    $("#tcName").val("");   //TC
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value =myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    ReturnVisit.search();
};

//今日预约
ReturnVisit.todaySearch = function () {
    var totalDay=(new Date(myDate.getFullYear(), parseInt(myDate.getMonth() + 1), 0)).getDate();
    console.log(totalDay);
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value =myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    ReturnVisit.search();
};

//明日预约
ReturnVisit.tomorrowSearch = function () {
    var myDates = new Date();
    myDates.setDate(myDates.getDate() + 1);
    var months;
    var days;
    if ((myDates.getMonth() + 1) < 10) {
        months = "0" + (myDates.getMonth() + 1);
    } else {
        months = "" + (myDates.getMonth() + 1);
    }
    if (myDates.getDate() < 10) {
        days = "0" + myDates.getDate();
    } else {
        days = "" + myDates.getDate();
    }
    document.getElementById("startTime").value = myDates.getFullYear() + "-" + months + "-" + days + " " + "00:00:00";
    document.getElementById("endTime").value = myDates.getFullYear() + "-" + months + "-" + days + " " + "23:59:59";
    ReturnVisit.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ReturnVisit.formParams = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['sreach'] = $("#sreach").val().trim();  //姓名   电话   微信  预约号
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['tcName'] = $("#tcName").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    return queryData;
}

$(function () {
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value =myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    //初始化下拉TC列表
    $.post(Feng.ctxPath + "/tc/getLists", function (data) {
        var txt = '<option value="">--请选择现场TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('tcName').innerHTML = txt;
        //状态单选
        var seletetcName = $('#tcNameValue').val();
        if (seletetcName != "") {
            $("#tcName option[value=" + seletetcName + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    var defaultColunms = ReturnVisit.initColumn();
    var table = new BSTable(ReturnVisit.id, "/returnVisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ReturnVisit.formParams());
    ReturnVisit.table = table.init();
    ReturnVisit.search();

    //采用定时器监听 时间内容是否变化
    var oldStartValue = $("#startTime").val();
    var oldEndtimeValue = $("#endTime").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != oldStartValue || $("#endTime").val() != oldEndtimeValue) {//如果时间发生变化，就触发查询事件
                ReturnVisit.search();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue = $("#startTime").val();
                oldEndtimeValue = $("#endTime").val();
            }
        }, 500);
});
